package me.jiatao.wan.ui.main.system

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hi.dhl.binding.databind
import com.hi.dhl.binding.viewbind
import com.nex3z.flowlayout.FlowLayout
import me.jiatao.wan.R
import me.jiatao.wan.common.ext.addViewList
import me.jiatao.wan.data.network.pojo.CategoryTreePojo
import me.jiatao.wan.databinding.ItemArticleBinding
import me.jiatao.wan.databinding.ItemLabelFlowBinding
import java.util.*

/**
 * @Author: taojia
 * @CreateDate: 2021/9/6 3:41 下午
 * @Version: 1.0
 * @Description:
 */

class CategoryTreeAdapter(
    private val categoryList: List<CategoryTreePojo>
) : RecyclerView.Adapter<CategoryTreeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layout = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_label_flow, parent, false)
        return ViewHolder(layout)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding(categoryList[position])
    }

    override fun getItemCount(): Int {
        return categoryList.size
    }


    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val binding by viewbind<ItemLabelFlowBinding>()
        private val random = Random()

        fun binding(category: CategoryTreePojo) {
            binding.apply {
                categoryName.text = category.name
                val color: Int = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
                categoryName.setBackgroundColor(color)

                flowLayout.addViewList(
                    category.children.map { category ->
                        category.name
                    }
                )
            }
        }
    }
}